<template>
  <div class="song_wrapper">
    <!-- 导航栏 -->
    <van-nav-bar
      title="歌单详情"
      left-text="返回"
      left-arrow
      @click-left="onClickLeft"
    />
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
    >
      <ul class="song_list">
        <li v-for="(item, index) in list" :key="index">
          <a href="javascript:;" @click="playMusic(item.id)"></a>
          <b @click="playMusic(item.id)">{{ item.name }}</b>
          <span v-if="item.mvid != 0" @click="playMV(item.mvid)"><i></i></span>
        </li>
      </ul>
    </van-list>
    <div class="audio_con">
      <audio
        ref="audio"
        @play="play"
        @pause="pause"
        :src="musicUrl"
        controls
        autoplay
        loop
        class="myaudio"
      ></audio>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      // 限制列表
      list: [],
      // 歌曲列表
      musicList: [],
      // 歌曲地址
      musicUrl: "",
      // 歌曲封面
      musicCover: "",
      // 歌曲评论
      //   hotComments: [],
      // 限定数量
      loading: false,
      finished: false,
      songnum: 0,
    };
  },
  created() {
    this.getSearch();
  },
  methods: {
    onClickLeft() {
      // 如果历史记录大于1,那么返回到上一层,如果历史记录小于等于1,返回到根路由
      window.history.length > 1 ? this.$router.go(-1) : this.$router.push("/");
    },
    getSearch() {
      console.log("这都没有？");
      console.log("接收到的：" + this.$route.params.query);
      var that = this;
      this.$request("get", "/search?keywords=" + this.$route.params.query).then(
        function (response) {
          console.log("请求回来的歌曲一堆堆");
          console.log(response);
          that.musicList = response.result.songs;
          that.songnum = response.result.songCount;
          // console.log(response.result.songs);
        }
      );
    },
    // 获取歌曲地址
    playMusic: function (musicId) {
      var that = this;
      console.log(musicId);
      // 获取歌曲地址
      this.$request("get", "/song/url?id=" + musicId).then(function (response) {
        console.log(response.data[0].url);
        that.musicUrl = response.data[0].url;
      });

      // 歌曲详情获取
      // this.$request("get", "/song/detail?ids=" + musicId).then(function (
      //   response
      // ) {
      //   console.log(response.songs[0].al.picUrl);
      //   that.musicCover = response.songs[0].al.picUrl;
      // });

      // 歌曲评论获取
      // this.$request("get", "/comment/hot?type=0&id=" + musicId).then(function (
      //   response
      // ) {
      //   console.log(response.hotComments);
      //   that.hotComments = response.hotComments;
      // });
    },
    play: function () {
      // console.log("play");
      this.isPlaying = true;
    },
    pause: function () {
      // console.log("pause");
      this.isPlaying = false;
    },
    onLoad() {
      setTimeout(() => {
        // 注意splice和slice方法的差异
        this.list = this.list.concat(this.musicList.splice(0, 18));
        // console.log(this.list);

        // 加载状态结束
        this.loading = false;

        // 数据全部加载完成
        if (this.list.length >= this.songnum) {
          this.finished = true;
        }
      }, 1000);
    },
  },
};
</script>

<style scoped>
/* .song_wrapper {
  display: flex;
  flex-direction: column;
  flex: 1;
} */
.song_list li {
  font-size: 20px;
  height: 60px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  padding-left: 10px;
}

.song_list li:nth-child(odd) {
  background-color: rgba(240, 240, 240, 0.8);
}

.song_list li a {
  display: block;
  width: 30px;
  height: 30px;
  background-image: url("../../images/play.png");
  background-size: 100%;
  margin-right: 5px;
  box-sizing: border-box;
}

.song_list li b {
  color: darkcyan;
}

.song_list li span i {
  display: block;
  width: 30px;
  height: 30px;
  background-size: 100%;
  cursor: pointer;
  background: url("../../images/table.png") no-repeat;
}

/* 固定底部播放器 */

.audio_con {
  position: fixed;
  bottom: 3rem;
  height: 50px;
  width: 100%;
  background-color: #f1f3f4;
  border: 1px solid #b3b3b3;
  height: 5rem;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 999;
}
.myaudio {
  bottom: 0;
  width: 95%;
  outline: none;
  background-color: #f1f3f4;
}
</style>